package com.cskaoyan.config;

import org.hibernate.validator.HibernateValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * @author stone
 * @date 2022/01/06 16:19
 */
@Configuration
public class WebConfiguration implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("*")
                .allowedHeaders("*");
    }
    //@Value可以将配置文件的内容自动注入到属性内
    // @Value("${SavePath.ProfilePhoto}")
    // private String ProfilePhotoPath;   //图片物理存储路径
    // @Value("${SavePath.ProfilePhotoMapper}")
    // private String ProfilePhotoMapperPath;   //图片映射路径
    // @Override
    // public void addResourceHandlers(ResourceHandlerRegistry registry){
    //     registry.addResourceHandler(ProfilePhotoMapperPath+"**").addResourceLocations("file:"+ProfilePhotoPath+"/");
    // }


    // @Bean
    // public CommonsMultipartResolver commonsMultipartResolver(){
    //
    //     CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
    //     return commonsMultipartResolver;
    // }

//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        String path = System.getProperty("user.dir") + "/src/main/webapp/wx/storage/fetch";
//        registry.addResourceHandler("/**").addResourceLocations("file:" + path)
//                .addResourceLocations();
//    }

    @Autowired
    LoginInterceptor loginInterceptor;
    @Autowired
    AnnoInterceptor annoInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //添加拦截的url
        List<String> pathList = new ArrayList<>();
        pathList.add("/wx/cart/**");
        pathList.add("/wx/collect/**");
        pathList.add("/wx/address/**");
        pathList.add("/wx/order/**");
        pathList.add("/wx/feedback/**");
        pathList.add("/wx/footprint/**");
        pathList.add("/wx/coupon/**");
        pathList.add("/wx/region/list");
        pathList.add("/wx/user/index");



        //过滤拦截
        List<String> excludePathList = new ArrayList<>();
        excludePathList.add("/wx/cart/goodscount");
        excludePathList.add("/wx/coupon/list");

        registry.addInterceptor(annoInterceptor).addPathPatterns("/admin/*/login");
        registry.addInterceptor(loginInterceptor).addPathPatterns(pathList).excludePathPatterns(excludePathList);
    }

}
